package com.my.test.java.poi;

import java.io.*;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.my.test.java.poi.Test.Sku;

public class ReadExcel {

	public static void test(String path) throws Exception{
		InputStream is = new FileInputStream(path);  
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
        is.close();
        HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(0);  
		System.out.println(hssfSheet.getLastRowNum());
	}
	public static void read(String path) {
		try{
			FileInputStream fis = new FileInputStream(path);
			XSSFWorkbook workbook = new XSSFWorkbook(fis);
			 fis.close();
			 /*读EXCEL文字内容*/
			 XSSFSheet sheet = workbook.getSheetAt(0);
			 int last = sheet.getLastRowNum();
			 for(int i=1;i<last;i++){
				  XSSFRow row =  sheet.getRow(i);
				  if(row.getCell(0)!=null){
			          row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
			      }
				  row.getCell(0);
			 }
			 System.out.println("last="+last);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public static  void readXls(String path) throws IOException {  
        InputStream is = new FileInputStream(path);  
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);  
        // 循环工作表Sheet  
        for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {  
            HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);  
            if (hssfSheet == null) {  
                continue;  
            }  
            // 循环行Row  
            for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {  
                HSSFRow hssfRow = hssfSheet.getRow(rowNum);  
                if (hssfRow == null) {  
                    continue;  
                }  
                HSSFCell xh = hssfRow.getCell(0);  
                if (xh == null) {
                    continue;  
                }  
                System.out.println(getValue(xh));
            }  
        }  
    } 
	private static String getValue(HSSFCell hssfCell) {  
        if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {  
            // 返回布尔类型的值  
            return String.valueOf(hssfCell.getBooleanCellValue());  
        } else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {  
            // 返回数值类型的值  
            return String.valueOf(hssfCell.getNumericCellValue());  
        } else {  
            // 返回字符串类型的值  
            return String.valueOf(hssfCell.getStringCellValue());  
        }  
    }
	  
	  public static void main(String[] args) throws IOException {        
		  try {
			test("C://模板1.xls");
		} catch (Exception e) {
			e.printStackTrace();
		}
      }
}
